package org.example.zhihuan.services;

import org.example.zhihuan.services.projectException.AssIdExistException;
import org.example.zhihuan.services.projectException.AssedException;
import org.example.zhihuan.util.Db;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Assessment {
    private static int ASS_WAIT = 0;    //初始值
    private static int ASS_FAIL = 1;    //投诉失败
    private static int ASS_PASS = 2;    //投诉通过
    private static int ASS_DELETE = 3;  //删除评论


    private int assId;                  //评价id
    private int assUserId;             //评价方id
    private int isAssUserId;           //被评价方id
    private String assContent;            //评价描述
    private int adminId;               //管理员id
    private int assState;               //评价的状态
    private Date assDate;                  //评价时间

    private boolean isSaved = false;        //初始值

    /**
     * 创建评论
     */
    public void creatAss(int assUserId, int isAssUserId, String assContent, Date date) {
        Assessment assessment = new Assessment();
        assessment.assUserId = assUserId;
        assessment.isAssUserId = isAssUserId;
        assessment.assContent = assContent;
        assessment.save();
    }

    /**
     * 将数据持久化
     */
    public void save() {
        StringBuilder sb = new StringBuilder();
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (isSaved) {
            sb.append("update assessment set ")
                    .append(" assuser_id=").append(assUserId).append(",")
                    .append(" isassuser_id = ").append(isAssUserId).append(",")
                    .append(" ass_content = '").append(assContent).append("',")
                    .append(" admin_id = ").append(adminId).append(",")
                    .append(" ass_state = ").append(assState).append(",")
                    .append(" ass_date = '").append(fm.format(assDate)).append("'")
                    .append(" where ass_id = ").append(assId);
        } else {
            sb.append("insert into assessment(ass_id,assuser_id,isassuser_id,ass_content,admin_id,ass_state,ass_date) values(")
                    .append(assId).append(",").append(assUserId).append(",").append(isAssUserId).append(",'")
                    .append(assContent).append("',").append(adminId).append(",").append(assState).append(",'")
                    .append(fm.format(assDate)).append("')");
            isSaved = true;
        }

        Db.execute(sb.toString());
    }

    /**
     * 获取评论对象
     *
     * @param assId
     * @return
     */
    public static Assessment getAssessment(int assId) {
        if (!Assessment.assIdExist(assId))
            throw new AssIdExistException("评论不存在");
        Assessment assessment = new Assessment();
        Db.read("select * from assessment where ass_id = " + assId, set -> {
            if (set.next()) {
                assessment.assId = set.getInt("ass_id");
                assessment.assUserId = set.getInt("assuser_id");
                assessment.isAssUserId = set.getInt("isassuser_id");
                assessment.assContent = set.getString("ass_content");
                assessment.adminId = set.getInt("admin_id");
                assessment.assState = set.getInt("ass_state");
                assessment.assDate = set.getTimestamp("ass_date");
                assessment.isSaved = true;
            }
            return null;
        });
        return assessment;
    }

    /**
     * 评论审核
     */
    public void assCheck(boolean check, int adminId) {
        if (check == true) {
            this.adminId = adminId;
            this.assState = ASS_PASS;
            this.save();
        } else {
            this.adminId = adminId;
            this.assState = ASS_FAIL;
            this.save();
        }
    }

    /**
     * 判断评论是否存在
     *
     * @param assId
     * @return
     */
    public static boolean assIdExist(int assId) {
        return Db.read("select 1 from assessment where ass_id = " + assId, ResultSet::next);
    }

    /**
     * 管理员删除评论
     *
     * @param adminId
     * @return
     */
    public boolean assAdminDelete(int adminId) {
        this.adminId = adminId;
        this.assState = ASS_DELETE;
        this.save();
        return true;
    }

    /**
     * 用户删除评论
     *
     * @param userId
     * @return
     */
    public boolean assUserDelete(int userId) {
        this.assUserId = userId;
        this.assState = ASS_DELETE;
        this.save();
        return true;
    }
}